// pages/receive/details/details.js
import api from '../../../utils/api';
import util from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canEdit: false,
    activeTab: 0,
    basicData: '', //基本信息
    invoiceList: [], // 发票
    taskList: [], // 任务号
    contractList: [], // 合同
    flowList: [], // 审批流程
    claimId: '', // 认领ID
    flowId: '', // 流程ID
    isInvalid: false, // 作废
    radioBillReason: '1', // 不需要关联发票理由
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //认领ID
    this.setData({
      claimId: options.claimId
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getData() {
    this.getBasicData()
    this.getTaskList()
    this.getinvoiceList()
    this.getContractList()
  },
  onChange(e) {
    console.log(e)
    if (e.detail.index == 1) {
      this.getFlow()
    }
  },
  // 基本信息
  getBasicData() {
    let that = this;
    api.getClaimById({
      data: {
        claimId: that.data.claimId
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          let now = new Date(res.data.data[0].claimDate);
          res.data.data[0].claimDate = util.formatTime(now).split(' ')[0];
          res.data.data[0].receiveMoney = util.formatFigure(res.data.data[0].receiveMoney)
          res.data.data[0].unclaimedMoney = util.formatFigure(res.data.data[0].unclaimedMoney)
          res.data.data[0].invoiceMoney = util.formatFigure(res.data.data[0].invoiceMoney)
          res.data.data[0].claimMoney = util.formatFigure(res.data.data[0].claimMoney)
          res.data.data[0].payableMoney = util.formatFigure(res.data.data[0].payableMoney)
          res.data.data[0].isScYqmd = res.data.data[0].isScYqmd===null?res.data.data[0].isScYqmd:res.data.data[0].isScYqmd+''
          res.data.data[0].isYcBmd = res.data.data[0].isYcBmd===null?res.data.data[0].isYcBmd:res.data.data[0].isYcBmd+''
          res.data.data[0].isOfflineClaimed = res.data.data[0].isOfflineClaimed===null?res.data.data[0].isOfflineClaimed:res.data.data[0].isOfflineClaimed+''
          that.setData({
            basicData: res.data.data[0],
            flowId: res.data.data[0].flowId,
            radioBill: res.data.data[0].needInvoice ? '1' : '0',
            radioBillReason: res.data.data[0].whyNoInvoice == '先到款未开票'||!res.data.data[0].whyNoInvoice ? '0' : res.data.data[0].whyNoInvoice == '已在线下开票'?'1':'2',
            radioContract: res.data.data[0].needContract ? '1' : '0'
          })
        }
      }
    })
  },
  // 查看逾期名单
  onOverduelist(){
    wx.navigateTo({
      url: '/pages/receive/overduelist/overduelist',
    })
  },
  // 查看白名单
  onWhitelist(){
    wx.navigateTo({
      url: '/pages/receive/whitelist/whitelist',
    })
  },
  // 发票
  getinvoiceList() {
    let that = this;
    api.getClaimInvoiceByClaimId({
      data: {
        claimId: that.data.claimId
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          for (let i = 0; i < res.data.data.length; i++) {
            res.data.data[i].money = util.formatFigure(res.data.data[i].money)
          }
          that.setData({
            invoiceList: res.data.data
          })
        }
      }
    })
  },
  // 任务号
  getTaskList() {
    let that = this;
    api.getClaimTaskByClaimId({
      data: {
        claimId: that.data.claimId
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          for (let i = 0; i < res.data.data.length; i++) {
            res.data.data[i].claimMoney = util.formatFigure(res.data.data[i].claimMoney)
          }
          that.setData({
            taskList: res.data.data
          })
        }
      }
    })
  },
  // 合同
  getContractList() {
    let that = this;
    api.getClaimContractByClaimId({
      data: {
        claimId: that.data.claimId
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          for (let i = 0; i < res.data.data.length; i++) {
            res.data.data[i].claimMoney = util.formatFigure(res.data.data[i].claimMoney)
          }
          that.setData({
            contractList: res.data.data
          })
        }
      }
    })
  },
  // 审批流程
  getFlow() {
    let that = this;
    api.getFlowByClaimId({
      data: {
        claimId: that.data.claimId
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          for (let i = 0; i < res.data.data.length; i++) {
            let now = res.data.data[i].flowStepDate ? new Date(res.data.data[i].flowStepDate) : '';
            res.data.data[i].flowStepDate = res.data.data[i].flowStepDate ? util.formatTime(now) : '';
          }
          that.setData({
            flowList: res.data.data,

          })
        }
      }
    })
  },
  // 查看作废详情
  onInvalidDetail() {
    wx.navigateTo({
      url: '/pages/receive/invalidDetails/invalidDetails?abolishId=' + this.data.basicData.abolishId + '&claimId=' + this.data.claimId,
    })
  },
  // 补充信息
  onAdditional(e) {
    wx.navigateTo({
      url: '/pages/receive/additionalInformation/additionalInformation?claimId=' + this.data.claimId + '&abolishId=' + this.data.basicData.abolishId + '&isCancle=' + e.currentTarget.dataset.cancle,
    })
  },
  // 作废
  onCancellation() {
    this.setData({
      isInvalid: true
    })
  },
  // 取消作废
  onCancleInvalid() {
    this.setData({
      isInvalid: false
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})